package com.hfzy.ihk.web.freeswitchManage.websocket.swx;

import com.hfzy.ihk.common.util.string.StringUtil;

import com.hfzy.ihk.web.freeswitchManage.websocket.swx.base.CommonWebSocketHandshakeInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/20.
 *
 * @author wws
 */
public class SwxWebSocketHandshakeInterceptor extends CommonWebSocketHandshakeInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(SwxWebSocketHandshakeInterceptor.class);

    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {

        /**连接安全验证*/
        boolean validateSource = super.beforeHandshake(request,response,wsHandler,attributes);

        if(!validateSource)
            return false;

        HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
        String serverId = servletRequest.getParameter("serverId");

        logger.info("swx websocket 链接 serverId:{}",serverId);

        if(StringUtil.isEmpty(serverId))
         return false;

        attributes.put("serverId",serverId);
        return true;
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
        logger.info("websocket after Handshake!");
    }

}
